<template>
  <section>
    <el-dialog
      :title="`${row.isAuth ? '审核' : '查看详情'}`"
      :visible.sync="open"
      width="600px"
      append-to-body
      @close="cancel"
      custom-class="task-mode"
    >
      <Form :formData="detailData" />
      <Form
        v-if="row.isAuth"
        :formData="formData"
        :formBtns="formBtns"
        @handleSubmit="handleSubmit"
      />
      <Process
        v-if="!row.isAuth && processData.length"
        :processData="processData"
        title="流转记录"
      />
    </el-dialog>
    <Audit :row="row" @onBack="onBack" @onSuccess="onSuccess" v-if="isAudit" />
  </section>
</template>

<script>
import CollectForm from "../collect/form.vue";
import Process from "../audit/process.vue";
import Form from "@/components/form/index.vue";
import Audit from "./audit.vue";
import { storage } from "@/utils/util.js";
import { regionData,  TextToCode } from "element-china-area-data";
import { vendorInfoDynamicForm } from "@/api/collect/collect/index";
import {
  vendorAuditGetInfo,
  vendorAuditResponsibilityAudit,
} from "@/api/collect/task/index";
export default {
  components: {
    CollectForm,
    Process,
    Form,
    Audit
  },
  props: {
    row: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      open: true,
      // 是否提交审核
      isAudit: false,
      detailData: [],
      processData: [],
      user: storage.getStorage("user") || {},
      formData: [
        {
          prop: "status",
          type: "select",
          options: [
            {
              label: "通过",
              value: "1",
            },
            {
              label: "拒绝",
              value: "0",
            },
          ],
          label: "审批结果",
          value: "1",
        },
        {
          prop: "reason",
          type: "textarea",
          label: "审批意见",
          rows: 3,
        },
      ],
      formBtns: [
        //表单按钮
        {
          type: "primary",
          label: "保存",
          isVerify: true,
        },
        {
          label: "关闭",
          click: () => {
            this.cancel();
          },
        },
      ],
      auditData:{},
      nextAuditType:[]
    };
  },

  mounted() {
    this.getTemplateForm();
  },
  methods: {
    //获取表单结构
    getTemplateForm() {
      this.loading = true;
      vendorInfoDynamicForm({deptId:this.row.deptId}).then((response) => {
        let data= response.data || [];
        data.forEach(item=>{
          if(item.prop=='checkAddress'){
            item.options=regionData;
          }
        })
        this.detailData = data;
        this.loading = false;
        if (this.row.id) {
          this.getLoad();
        }
      });
    },
    //获取数据
    getLoad() {
      vendorAuditGetInfo({ id: this.row.id }).then((response) => {
        const data = response.data;
        if (data.checkAddress&&data.checkAddress.includes("/")) {
          let checkAddress = data.checkAddress.split("/");
          data["checkAddress"] = [
            TextToCode[checkAddress[0]].code,
            TextToCode[checkAddress[0]][checkAddress[1]].code,
            TextToCode[[checkAddress[0]]][checkAddress[1]][checkAddress[2]].code,
          ];
        }
        this.nextAuditType=data.nextAuditType;
        this.detailData = this.UpdataForm(data, this.detailData, true);
        this.processData = data.auditList;
      });
    },

    //保存
    handleSubmit(data) {
      this.auditData = data;
      //分管领导审核，不需要再选择人员，截止审核
      let flag = false;
      if(this.row.authType==='reviewer'&&this.nextAuditType=='1'){
        flag = true;
      }
      if(this.row.authType==='department'&&(this.nextAuditType=='2'||this.nextAuditType=='1,2')){
        flag = true;
      }
      if(this.row.authType==='leader' || flag){
        let params = {
          id: this.row.id,
          ...this.auditData
        };
        vendorAuditResponsibilityAudit(params).then((response) => {
          this.Message("审核成功");
          this.isAudit = false;
          this.$emit("onSuccess", "isDetail");
        });
        return;
      }
      this.isAudit = true;

    },
    // 关闭模态框
    cancel() {
      this.$emit("onBack", "isDetail");
    },
    /** 成功回调 */
    onSuccess(data) {
      let array=data.nextAudit.split('/');
      let params = {
        id: this.row.id,
        ...this.auditData,
        nextAuditId:array[0],
        nextAuditName:array[1]
      };

      vendorAuditResponsibilityAudit(params).then((response) => {
        this.Message("审核成功");
        this.isAudit = false;
        this.$emit("onSuccess", "isDetail");
      });
    },
    /** 关闭弹窗  */
    onBack() {
      this.isAudit = false;
    },
  },
};
</script>

<style lang="scss">
@import "../../../assets/styles/collect.scss";
.task-mode {
  .el-dialog__body {
    padding: 0 20px 15px;
    max-height: 600px;
    overflow-y: auto;
  }
  .form-panel {
    margin: 0;
  }
  .el-timeline {
    width: 90%;
  }
  .form {
    margin: 0;
    .el-form-item {
      width: 100%;
      .el-form-item__label {
        width: 130px !important;
      }
      .el-form-item__content {
        margin-left: 130px !important;
      }
      &:last-child {
        margin-bottom: 0 !important;
      }
    }
  }
}
</style>
